// pages/commit/commit.js
//获取应用实例
const app = getApp()
var util= require("../../utils/libs.js");
var api = require("../../utils/api.js");
var date = require("../../utils/date.js");
Page({
  data: {
    id:null,
    value:"",
    content:"",
    page:1,
    limit:10, 
    commits: [],
    param:''
  },
  detail: function (event) {
    var id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../article/article?id=' + id,
    })
  },
  editP: function () {
    wx.navigateTo({
      url: '../groups/groups',
    })
  },
  onLoad(options){
    let that = this
    let uid = wx.getStorageSync('uid')
    that.setData({
      id:options.id,
      uid:uid
    })
    that.param = options.param
    that.articleList()
  },
  articleList(){
    let that = this
    let apis;
    if(that.param !=''){
      apis = api.articleR
    }else{
      apis = api.topicCommentList
    }
    let uids = wx.getStorageSync('uid')
    util.request(apis,{
      id:that.data.id,
      uid:uids,
      page:that.data.page,
      limit:that.data.limit 
    },"GET")
    .then(function (res) {
      if(res.error_code === 0){
       let arr = res.data.list
       arr.forEach(e => {
        e.create_time = date.data(e.create_time)
       });
       that.setData({
         commits:arr
       })
      }
    })
  },
  bindKeyInput(e){
    let vaule = e.detail.value
    let that = this
    that.setData({
      content:vaule
    })
  },
  handlesubmit(){
    let that = this
    util.request(api.topicCommentAdd,{
      id:that.data.id,
      uid:that.data.uid,
     content:that.data.content
    },"POST")
    .then(function (res) {
      if(res.error_code === 0){
        wx.showToast({
          title: '评论成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function(){
          that.setData({
            value: ""
          })
          that.articleList()
        },3000)
      } 
    })
  }
})
